﻿#define DB_MYSQL
//#define DB_ACCESS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DAO;
using DAO.DB;

using Code;

public partial class SubmitStartTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            var id_test = this.Request["testID"];
            var id_student = this.Request["Id_Usuario"];
            var mode = this.Request["Mode"];
            var cpf = this.Request["cpf"];

#if DB_MYSQL
            DAO.DAO.Instance.Setup(DatabaseTypes.MySql, DataHelper.Instance.MySqlConnectionString);
#else
            // Setup the Access database
            DAO.DAO.Instance.Setup(DatabaseTypes.Access, DataHelper.Instance.AccessConnectionString + this.Server.MapPath("banco.mdb"));
#endif

            //DAO.DAO.Instance.Connector.Open();
            //DAO.DAO.Instance.Connector.Execute("INSERT INTO tb_teste (conteudo) VALUES ('" + id_test + "-" + id_student + "-" + mode + "-" + cpf + "')");
            //DAO.DAO.Instance.Connector.Close();

            if (string.IsNullOrEmpty(cpf) == true || cpf == "0" || cpf.Length != 11 || cpf.Contains(' ') == true)
            {
                cpf = "00000000000";
            }

            int? modeValue = null;
            if (string.IsNullOrEmpty(id_test) == true)
            {
                this.Response.Write("Parametros invalidos. #1");
                return;
            }

            if (string.IsNullOrEmpty(id_student) == true && string.IsNullOrEmpty(mode))
            {
                this.Response.Write("Parametros invalidos. #2");
                return;
            }

            if (string.IsNullOrEmpty(mode) != true)
            {
                modeValue = int.Parse(mode);

                if (modeValue.Value == 1)
                {
                    return;
                }
            }

            List<String> columns = new List<string>();

            // To see if the student already started the test.
            columns.Clear();
            columns.Add("startDate");

            DAO.DAO.Instance.Connector.Open();
            List<Dictionary<string, object>> data = DAO.DAO.Instance.Connector.QueryWithColumns("select startDate from tb_TestStatus WHERE Excluido <> 1 AND id_test=" + id_test + " AND Id_Usuario=" + id_student + " AND cpf='" + cpf + "'", columns);
            DAO.DAO.Instance.Connector.Close();

            object startDateValue = null;
            if (data[0].TryGetValue("startDate", out startDateValue) == true)
            {
                if (string.IsNullOrEmpty(startDateValue as string) == false)
                {
                    return;
                }
            }

            if (modeValue == 0)
            {
                DAO.DAO.Instance.Connector.Open();
                DAO.DAO.Instance.Connector.Execute("update tb_TestStatus SET startDate='" + DateTime.Now + "' WHERE Excluido <> 1 AND id_test=" + id_test + " AND Id_Usuario=" + id_student + " AND cpf=" + cpf);
                DAO.DAO.Instance.Connector.Close();
            }
        }
        catch (Exception error)
        {
            this.Response.Write(error.Message + " - " + error.StackTrace);
        }
    }
}